//
//  HMHomeClickViewController.m
//  LoveFreshBeen
//
//  Created by huodian on 16/10/19.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "HMHomeClickViewController.h"

#import "HMHomeHeadView.h"

#import <UIImageView+WebCache.h>

#import "HMHomeFirstModel.h"

#import "ShopCartGoodsListTool.h"

#import <UMSocialCore/UMSocialCore.h>


@interface HMHomeClickViewController ()


@property(nonatomic,strong)HMHomeHeadView* headView;

@property(nonatomic,strong)UIView* view1;

@property(nonatomic,strong)UILabel* countLable;

@property(nonatomic,strong)UIButton* reduceLable;

@property(nonatomic,strong)UIButton* countBtn;



@end

@implementation HMHomeClickViewController

-(HMHomeHeadView *)headView
{
    if (_headView == nil) {
    
        _headView = [[HMHomeHeadView alloc]init];
        
    }
    return  _headView;
    
}

-(void)setModel:(HMHomeFirstModel *)model
{
    _model = model;
    
    self.headView.model = model;
    
    self.navigationItem.title = model.name;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self setMyUI];
    
    
    [self addBackButton];
    
   
}
-(void)setMyUI{
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    
    CGFloat imageWidth = [UIScreen mainScreen].bounds.size.width;
    
    UIView * view1 = [[UIView alloc]init];
      [self.view addSubview:view1];
    
       view1.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.bottom.trailing.equalTo(self.view);
        
        make.height.equalTo(@64);
        
    }];
    
    
#pragma mark ----- 添加底部按钮

    UILabel* lable = [[UILabel alloc]init];
    
    lable.text = @"添加商品:";
    
    [view1 addSubview:lable];
    
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(view1).offset(10);
        
        make.centerY.equalTo(view1.mas_centerY);
        
        make.width.equalTo(@80);
        
        make.height.equalTo(@20);
        
        
    }];
    
    UIButton* reduceBtn = [[UIButton alloc]init];
    
    self.reduceLable = reduceBtn;
    
//    reduceBtn.hidden = YES;
    
    [reduceBtn setBackgroundImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    
    reduceBtn.tag = 30;
    
     [reduceBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [view1 addSubview: reduceBtn];
    
    [reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(view1.mas_centerY);
        
        make.leading.equalTo(lable.mas_trailing).offset(5);
        
        make.width.height.equalTo(@30);
        
        
    }];
    
    UILabel* lable1 = [[UILabel alloc]init];
    
    self.countLable = lable1;
    
    [view1 addSubview:lable1];
    lable1.text = @"0";
    
    [lable1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(reduceBtn.mas_trailing).offset(5);
        
        make.centerY.equalTo(view1.mas_centerY);
        
        make.width.equalTo(@30);
        
        make.height.equalTo(@20);
        
        
    }];
    
    UIButton* addBtn = [[UIButton alloc]init];
    
    [addBtn setBackgroundImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    
    addBtn.tag = 40;
    
    [addBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [view1 addSubview: addBtn];
    
    
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(view1.mas_centerY);
        
        make.leading.equalTo(lable1.mas_trailing).offset(5);
        
        make.width.height.equalTo(@30);
        
        
    }];
    
    
    UIButton* shopBtn = [[UIButton alloc]init];
    
   
    
    [shopBtn  setBackgroundImage:[UIImage imageNamed:@"v2_shop_empty"] forState:UIControlStateNormal];

    
    [self.view addSubview:shopBtn];
    
    [shopBtn  mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.view).offset(-30);
        
        make.trailing.equalTo(view1).offset(-20);
        
        make.width.height.equalTo(@60);
        
        
    }];
    
    
    UIButton* countBtn = [[UIButton alloc]init];
    
    countBtn.hidden = YES;
    
    self.countBtn = countBtn;
    
//    [countBtn setTitle:@"10" forState:UIControlStateNormal];
    
    countBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [countBtn setBackgroundImage:[UIImage imageNamed:@"reddot"] forState:UIControlStateNormal];
    
    [self.view addSubview:countBtn];
    
    [countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.trailing.equalTo(shopBtn.mas_leading).offset(21);
        
        make.top.equalTo(shopBtn.mas_top);
        
        make.width.height.equalTo(@20);
        
    }];
    
    
    
    
    
    
    UIScrollView* table = [[UIScrollView alloc]init];
    
    table.bounces = NO;
    
    table.contentSize = CGSizeMake(imageWidth, 1325);
    
    [self.view addSubview:table];
    
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.trailing.equalTo(self.view);
        
        make.bottom.equalTo(view1.mas_top);
        
        
    }];
//
//    self.headView = [[HMHomeHeadView alloc]init];
    

    
//    [self.view addSubview: self.headView ];
    
    [table addSubview:self.headView];
    
//    table.tableHeaderView.backgroundColor = [UIColor greenColor];
    
    [ self.headView  mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.trailing.equalTo(table);
        
        make.height.equalTo(@625);
        
    }];
    
    
    
    UIImageView* imageView = [[UIImageView alloc]init];
    
    [table addSubview:imageView];
    
    imageView.image = [UIImage imageNamed:@"aaaa"];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.equalTo(@(imageWidth));
        
        make.top.equalTo(self.headView.mas_bottom);
        
        make.bottom.left.right.equalTo(table);
        
    }];
    
    
     [self.view bringSubviewToFront:shopBtn];
    
     [self.view bringSubviewToFront:countBtn];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)clickBtn:(UIButton*)btn
{
    if (btn.tag == 40)
    {
        
        
        if (self.model.number > [self.countLable.text integerValue]) {
           
            self.model.goodsCount ++;
            
            self.countLable.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
            
//            self.reduceLable.hidden = NO;
            
            self.countBtn.hidden = NO;
            
          
            
            [UIView animateWithDuration:0.25 animations:^{
                
                self.countBtn.transform = CGAffineTransformMakeScale(1.5, 1.5);
                
            } completion:^(BOOL finished) {
               
                [UIView animateWithDuration:0.25 animations:^{
                    
                    self.countBtn.transform = CGAffineTransformMakeScale(1, 1);
                } completion:^(BOOL finished) {
                    
                      [self.countBtn setTitle:self.countLable.text forState:UIControlStateNormal];
                    
                }];
                
                
            }];
            
           
            
        }else
        {
            
            [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:@"你大爷还点,老子又不是点读机"];
            
            [SVProgressHUD setMinimumDismissTimeInterval:1];
            
            [SVProgressHUD setDefaultStyle:  SVProgressHUDStyleCustom];
            
            btn.enabled = NO;
            
      
        }
        btn.enabled = YES;
        
        
        //        [[NSNotificationCenter defaultCenter] postNotificationName:@"add" object:self.model];
        
        
        
        
        
        self.countLable.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
        self.reduceLable.hidden = NO;
//        self.countLable.hidden = NO;
        //添加模型到单例对象的数组中
        [[ShopCartGoodsListTool sharedGoodsListTool] addGoodsToShopCartGoodsList:self.model];
        
        
        
    }else if(btn.tag == 30)
    {
        
        
        
        
        
        self.model.goodsCount --;
        self.countLable.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
        
          [self.countBtn setTitle:self.countLable.text forState:UIControlStateNormal];
        if (self.model.goodsCount == 0) {
            
//            self.reduceLable.hidden = YES;
//            self.countLable.hidden = YES;
            self.countBtn.hidden = YES;
        }
        
        [[ShopCartGoodsListTool sharedGoodsListTool] reduceGoodsFromShopCartGoodsList:self.model];
        
    }
    
   
    
    if (self.model.number >= [self.countLable.text integerValue]) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:KChangeGoodCountNoti object:self.model];
        
    }
    
    
    
    
}

- (void)addBackButton {
    
    self.title = @"扫描店铺二维码";
    UIButton *back = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    [back setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(share)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor lightGrayColor]];
}

- (void)share {
    NSLog(@"fenxiangnimabi");
    
    UIAlertController *sheetView = [UIAlertController alertControllerWithTitle:@"分享到" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"微信好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"========哈哈哈哈哈");
    }];
    
    [sheetView addAction:alert1];
    
    UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"微信圈好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self getUserInfoFromSina];
    }];
    
    [sheetView addAction:alert2];
    
    UIAlertAction *alert3 = [UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self shareTextToSina];
        
    }];
    
    [sheetView addAction:alert3];
    
    UIAlertAction *alert4 = [UIAlertAction actionWithTitle:@"QQ空间" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self getAuthInfoFromSina];
    }];
    
    [sheetView addAction:alert4];
    
    UIAlertAction *alert5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [sheetView addAction:alert5];
    
    [self presentViewController:sheetView animated:YES completion:nil];
    

}

-(void)getAuthInfoFromSina
{
    [[UMSocialManager defaultManager]  authWithPlatform:UMSocialPlatformType_Sina currentViewController:self completion:^(id result, NSError *error) {
        
        UMSocialAuthResponse *authresponse = result;
        NSString *message = [NSString stringWithFormat:@"result: %d\n uid: %@\n accessToken: %@\n",(int)error.code,authresponse.uid,authresponse.accessToken];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Login" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
    }];
}

- (void)getUserInfoFromSina
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:self completion:^(id result, NSError *error) {
        UMSocialUserInfoResponse *userinfo =result;
        NSString *message = [NSString stringWithFormat:@"name: %@\n icon: %@\n gender: %@\n",userinfo.name,userinfo.iconurl,userinfo.gender];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"UserInfo" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
    }];
}

- (void)shareTextToSina
{
    NSString *text = @"神呀,救救我吧,一把年纪了,还是一个人!....";
    
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    messageObject.text = text;
    
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        NSString *message = nil;
        if (!error) {
            message = [NSString stringWithFormat:@"分享成功"];
        } else {
            message = [NSString stringWithFormat:@"失败原因Code: %d\n",(int)error.code];
            
        }
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"share" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
    }];
}



- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:254/255.0 green:211/255.0 blue:65/255.0 alpha:1]];
    
}





@end
